package com.it.controller;

import com.aliyun.oss.OSSClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author zhangyan
 * @version 1.0
 * @date 2019/7/11 15:44
 */
@RestController
@RequestMapping("/upload")
public class uploadController {
//
//    @Autowired
//    private HttpServletRequest request;
//    @PostMapping("/ups")
//    public String up(@RequestParam("file") MultipartFile file) throws IOException {
//        String img = request.getSession().getServletContext().getRealPath("img");
//
//        String originalFilename = file.getOriginalFilename();
//
//        String descfilename= img+"/"+originalFilename;
//
//        File descfile= new File(descfilename);
//   if(!descfile.getParentFile().exists()){
//       descfile.mkdirs();
//   }
//        try {
//            file.transferTo(descfile);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        file.getInputStream().close();
//
//    return "http://localhost:9001/web/img/"+originalFilename;
//    }



    @Autowired
    private OSSClient ossClient;

    @PostMapping("/oss")
    public String ossUpload(@RequestParam("file") MultipartFile file,String folder){
        String bucketName="greenoranges";
        String fileName = folder+"/"+ UUID.randomUUID()+ file.getOriginalFilename();
        try {
            ossClient.putObject(bucketName,fileName,file.getInputStream());
        } catch (IOException e) {
        }
        ossClient.shutdown();
        return "https://"+bucketName+".oss-cn-hangzhou.aliyuncs.com/"+fileName;
    }
}
